<template>
  <el-dialog title="采购定价" width="30%" :visible.sync="visible">
    <el-form ref="form" :model="form" class="dialog-form" label-position="right" label-width="120px">
      <el-form-item label="验收金额">
        <label class="money">￥{{ form.params.actualAmount.toFixed(2) }}</label>
      </el-form-item>
      <el-form-item label="定价">
        <label class="money">￥{{ form.params.paymentAmount.toFixed(2) }}</label>
      </el-form-item>
      <el-form-item label="运费" prop="params.freight" :rules="self.page.rules.num">
        <el-input-number v-model="form.params.freight" controls-position="right" :precision="2" :max="form.params.actualAmount" :min="0" @change="cal" />
      </el-form-item>
      <el-form-item label="罚款" prop="params.penalty" :rules="self.page.rules.num">
        <el-input-number v-model="form.params.penalty" controls-position="right" :precision="2" :max="form.params.actualAmount" :min="0" @change="cal" />
      </el-form-item>
      <el-form-item label="业务费" prop="params.expense" :rules="self.page.rules.num">
        <el-input-number v-model="form.params.expense" controls-position="right" :precision="2" :max="form.params.actualAmount" :min="0" @change="cal" />
      </el-form-item>
      <el-form-item label="其他" prop="params.other" :rules="self.page.rules.num">
        <el-input-number v-model="form.params.other" controls-position="right" :precision="2" :max="form.params.actualAmount" :min="0" @change="cal" />
      </el-form-item>
      <el-form-item label="补充描述" prop="message">
        <el-input v-model="form.message" type="textarea" :rows="2" placeholder="请输入补充描述" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="mini" @click="visible = false">取 消</el-button>
      <el-button size="mini" type="primary" @click="submit">提交</el-button>
    </div>
  </el-dialog>
</template>
<script>
export default {
  name: 'PurPricing',
  props: {
    value: {
      type: String,
      default: ''
    },
    self: {
      type: Object,
      default: () => { return {} }
    }
  },
  data() {
    return {
      visible: false,
      form: {
        id: 0,
        message: '',
        params: {
          actualAmount: 0,
          paymentAmount: 0,
          freight: 0,
          penalty: 0,
          expense: 0,
          other: 0,
          profit: 0
        }
      }
    }
  },
  created() {
  },
  mounted() {
    this.form.id = this.value
  },
  methods: {
    show(orderPrice, actualAmount) {
      this.visible = true
      // this.orderPrice = actualAmount
      this.form.params.actualAmount = actualAmount
      this.form.params.paymentAmount = actualAmount
    },
    submit() {
      const that = this
      this.form.params.profit = this.form.params.actualAmount -
                                this.form.params.freight -
                                this.form.params.penalty -
                                this.form.params.expense -
                                this.form.params.other
      if (this.form.params.profit < 0) {
        that.$message.error('存在非法支出金额!')
        return
      }
      that.$refs['form'].validate((valid) => {
        if (!valid) {
          return
        }
        const loading = that.$loading()
        const url = that.self.page.router.remoteUrl + '/pricing'
        that.self.service.post(url, that.form).then(res => {
          loading.close()
          if (res.code !== 200) {
            that.$message.error(res.message)
            return
          }
          that.$message.success('操作成功!')
          that.self.reload(true)
        }).catch((e) => {
          loading.close()
        })
      })
    },
    cal() {
      this.form.params.profit = this.form.params.actualAmount -
                                this.form.params.freight -
                                this.form.params.penalty -
                                this.form.params.expense -
                                this.form.params.other
      this.form.params.paymentAmount = this.form.params.profit
      // console.log(this.form.params.paymentAmount)
    }
  }
}
</script>
